﻿using Autofac;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using xjjxmm.infrastructure.Authorizations;
using xjjxmm.infrastructure.SetUp;

namespace xjjxmm.infrastructure.compont;

public class AuthorizationCompont : ICompont
{
    public int Order { get; set; } = 650;

    public bool Regist(WebApplicationBuilder builder) {
        var requirement = new XjjXmmRequirement();
        //builder.Services.AddSingleton<IAuthorizationHandler, XjjXmmAuthorizationHandler>();
        //builder.Services.AddSingleton(requirement);
        builder.Services.AddAuthorization(options =>
        {
            // 自定义基于策略的授权权限
            options.AddPolicy("Permission",
                policy => policy.Requirements.Add(requirement));
        });

       

        return false;
    }

    public bool Regist(ContainerBuilder builder)
    {

        builder.RegisterType<JwtAuthenticationMiddleware>().SingleInstance();
        return false;
    }

    public bool Use(WebApplication app)
    {
       

        //授权
        app.UseAuthorization();

        app.MapControllers();
        //.RequireAuthorization("Permission");

        app.UseMiddleware<JwtAuthenticationMiddleware>();

        return true;
    }
}
